#pragma once

#include"LogIn.h"

namespace mpk_internal_system {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	using namespace MySql::Data::MySqlClient;

	/// <summary>
	/// Summary for ChangePasswordWindow
	/// </summary>
	public ref class ChangePasswordWindow : public System::Windows::Forms::Form
	{
	public:
		bool succeed_changed;
		sLogIn ^ logger;

		ChangePasswordWindow(void)
		{
			InitializeComponent();
			
			succeed_changed = false;
			logger = gcnew sLogIn();
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~ChangePasswordWindow()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  menu;
	private: System::Windows::Forms::TextBox^  old;
	private: System::Windows::Forms::TextBox^  newPSWD;
	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::Label^  label3;
	private: System::Windows::Forms::TextBox^  confirm;
	private: System::Windows::Forms::Button^  change;
	protected: 

	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->menu = (gcnew System::Windows::Forms::Button());
			this->old = (gcnew System::Windows::Forms::TextBox());
			this->newPSWD = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->label3 = (gcnew System::Windows::Forms::Label());
			this->confirm = (gcnew System::Windows::Forms::TextBox());
			this->change = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// menu
			// 
			this->menu->Location = System::Drawing::Point(197, 227);
			this->menu->Name = L"menu";
			this->menu->Size = System::Drawing::Size(75, 23);
			this->menu->TabIndex = 0;
			this->menu->Text = L"Menu";
			this->menu->UseVisualStyleBackColor = true;
			this->menu->Click += gcnew System::EventHandler(this, &ChangePasswordWindow::menu_Click);
			// 
			// old
			// 
			this->old->Location = System::Drawing::Point(107, 34);
			this->old->Name = L"old";
			this->old->PasswordChar = '*';
			this->old->Size = System::Drawing::Size(100, 20);
			this->old->TabIndex = 1;
			// 
			// newPSWD
			// 
			this->newPSWD->Location = System::Drawing::Point(107, 60);
			this->newPSWD->Name = L"newPSWD";
			this->newPSWD->PasswordChar = '*';
			this->newPSWD->Size = System::Drawing::Size(100, 20);
			this->newPSWD->TabIndex = 2;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(30, 37);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(71, 13);
			this->label1->TabIndex = 3;
			this->label1->Text = L"Old password";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(23, 63);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(78, 13);
			this->label2->TabIndex = 4;
			this->label2->Text = L"New Password";
			// 
			// label3
			// 
			this->label3->AutoSize = true;
			this->label3->Location = System::Drawing::Point(12, 89);
			this->label3->Name = L"label3";
			this->label3->Size = System::Drawing::Size(91, 13);
			this->label3->TabIndex = 5;
			this->label3->Text = L"Confirm Password";
			// 
			// confirm
			// 
			this->confirm->Location = System::Drawing::Point(107, 86);
			this->confirm->Name = L"confirm";
			this->confirm->PasswordChar = '*';
			this->confirm->Size = System::Drawing::Size(100, 20);
			this->confirm->TabIndex = 6;
			// 
			// change
			// 
			this->change->Location = System::Drawing::Point(132, 112);
			this->change->Name = L"change";
			this->change->Size = System::Drawing::Size(75, 23);
			this->change->TabIndex = 7;
			this->change->Text = L"Change";
			this->change->UseVisualStyleBackColor = true;
			this->change->Click += gcnew System::EventHandler(this, &ChangePasswordWindow::change_Click);
			// 
			// ChangePasswordWindow
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(284, 262);
			this->Controls->Add(this->change);
			this->Controls->Add(this->confirm);
			this->Controls->Add(this->label3);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->newPSWD);
			this->Controls->Add(this->old);
			this->Controls->Add(this->menu);
			this->Name = L"ChangePasswordWindow";
			this->Text = L"ChangePasswordWindow";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void menu_Click(System::Object^  sender, System::EventArgs^  e) {
				 succeed_changed = true;
			 }
	private: System::Void change_Click(System::Object^  sender, System::EventArgs^  e) {
				 if(logger->user == "admin"){
					 if(old->Text->ToString() == logger->passwordAdmin && newPSWD->Text->ToString() == confirm->Text->ToString()){
						 logger->passwordAdmin = newPSWD->Text->ToString();
						 logger->changePSWD = true;
						 MessageBox::Show("Password changed");
					 }
					 else MessageBox::Show("Invalid password!");
 				 }
				 else if(logger->user == "employee"){
					 if(old->Text->ToString() == logger->password && newPSWD->Text->ToString() == confirm->Text->ToString()){
						System::String ^ strCon = "datasource=localhost;username=root;password=;database=mpk_db";
						System::String ^ strCommend = "UPDATE pracownik SET haslo='" + newPSWD->Text->ToString() + "' WHERE id_pracownik=" + logger->id;

						MySqlConnection ^ conDatabase = gcnew MySqlConnection(strCon);
						MySqlCommand    ^ cmdDatabase = gcnew MySqlCommand(strCommend, conDatabase);

						conDatabase->Open();

						cmdDatabase->ExecuteNonQuery();
						conDatabase->Close();
						MessageBox::Show("Password changed");
					 }
					 else MessageBox::Show("Invalid password!");
				 }
			 }
};
}
